using System; 
using System.Text; 
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration; 
using System.Xml; 
using System.Xml.Serialization;
using SubSonic; 
using SubSonic.Utilities;

namespace SubSonic.Security.Data
{
	/// <summary>
	/// Strongly-typed collection for the UsersInRole class.
	/// </summary>
	[Serializable]
	public partial class UsersInRoleCollection : ActiveList<UsersInRole, UsersInRoleCollection> 
	{	   
		public UsersInRoleCollection() {}

	}

	/// <summary>
	/// This is an ActiveRecord class which wraps the UsersInRoles table.
	/// </summary>
	[Serializable]
	public partial class UsersInRole : ActiveRecord<UsersInRole>
	{
		#region .ctors and Default Settings
		
		public UsersInRole()
		{
		  SetSQLProps();
		  InitSetDefaults();
		  MarkNew();
		}

		
		private void InitSetDefaults() { SetDefaults(); }

		
		public UsersInRole(bool useDatabaseDefaults)
		{
			SetSQLProps();
			if(useDatabaseDefaults)
				ForceDefaults();
			MarkNew();
		}

		public UsersInRole(object keyID)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByKey(keyID);
		}

		 
		public UsersInRole(string columnName, object columnValue)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByParam(columnName,columnValue);
		}

		
		protected static void SetSQLProps() { GetTableSchema(); }

		
		#endregion
		
		#region Schema and Query Accessor
		public static Query CreateQuery() { return new Query(Schema); }

		
		public static TableSchema.Table Schema
		{
			get
			{
				if (BaseSchema == null)
					SetSQLProps();
				return BaseSchema;
			}

		}

		
		private static void GetTableSchema() 
		{
			if(!IsSchemaInitialized)
			{
				//Schema declaration
				TableSchema.Table schema = new TableSchema.Table("UsersInRoles", TableType.Table, DataService.GetInstance("SubSonicProviders"));
				schema.Columns = new TableSchema.TableColumnCollection();
				schema.SchemaName = @"dbo";
				//columns
				
				TableSchema.TableColumn colvarId = new TableSchema.TableColumn(schema);
				colvarId.ColumnName = "ID";
				colvarId.DataType = DbType.Int32;
				colvarId.MaxLength = 0;
				colvarId.AutoIncrement = true;
				colvarId.IsNullable = false;
				colvarId.IsPrimaryKey = true;
				colvarId.IsForeignKey = false;
				colvarId.IsReadOnly = false;
				colvarId.DefaultSetting = @"";
				colvarId.ForeignKeyTableName = "";
				schema.Columns.Add(colvarId);
				
				TableSchema.TableColumn colvarRoleID = new TableSchema.TableColumn(schema);
				colvarRoleID.ColumnName = "RoleID";
				colvarRoleID.DataType = DbType.Guid;
				colvarRoleID.MaxLength = 0;
				colvarRoleID.AutoIncrement = false;
				colvarRoleID.IsNullable = false;
				colvarRoleID.IsPrimaryKey = false;
				colvarRoleID.IsForeignKey = true;
				colvarRoleID.IsReadOnly = false;
				colvarRoleID.DefaultSetting = @"";
				
					colvarRoleID.ForeignKeyTableName = "Roles";
				schema.Columns.Add(colvarRoleID);
				
				TableSchema.TableColumn colvarUserID = new TableSchema.TableColumn(schema);
				colvarUserID.ColumnName = "UserID";
				colvarUserID.DataType = DbType.Guid;
				colvarUserID.MaxLength = 0;
				colvarUserID.AutoIncrement = false;
				colvarUserID.IsNullable = false;
				colvarUserID.IsPrimaryKey = false;
				colvarUserID.IsForeignKey = true;
				colvarUserID.IsReadOnly = false;
				colvarUserID.DefaultSetting = @"";
				
					colvarUserID.ForeignKeyTableName = "Users";
				schema.Columns.Add(colvarUserID);
				
				BaseSchema = schema;
				//add this schema to the provider
				//so we can query it later
				DataService.Providers["SubSonicProviders"].AddSchema("UsersInRoles",schema);
			}

		}

		#endregion
		
		#region Props
		
		  
		[XmlAttribute("Id")]
		public int Id 
		{
			get { return GetColumnValue<int>("ID"); }

			set { SetColumnValue("ID", value); }

		}

		  
		[XmlAttribute("RoleID")]
		public Guid RoleID 
		{
			get { return GetColumnValue<Guid>("RoleID"); }

			set { SetColumnValue("RoleID", value); }

		}

		  
		[XmlAttribute("UserID")]
		public Guid UserID 
		{
			get { return GetColumnValue<Guid>("UserID"); }

			set { SetColumnValue("UserID", value); }

		}

		
		#endregion
		
		
			
		
		#region ForeignKey Properties
		
		/// <summary>
		/// Returns a Role ActiveRecord object related to this UsersInRole
		/// 
		/// </summary>
		public SubSonic.Security.Data.Role Role
		{
			get { return SubSonic.Security.Data.Role.FetchByID(this.RoleID); }

			set { SetColumnValue("RoleID", value.RoleID); }

		}

		
		
		/// <summary>
		/// Returns a User ActiveRecord object related to this UsersInRole
		/// 
		/// </summary>
		public SubSonic.Security.Data.User User
		{
			get { return SubSonic.Security.Data.User.FetchByID(this.UserID); }

			set { SetColumnValue("UserID", value.UserID); }

		}

		
		
		#endregion
		
		
		
		//no ManyToMany tables defined (0)
		
		#region ObjectDataSource support
		
		
		/// <summary>
		/// Inserts a record, can be used with the Object Data Source
		/// </summary>
		public static void Insert(Guid varRoleID,Guid varUserID)
		{
			UsersInRole item = new UsersInRole();
			
			item.RoleID = varRoleID;
			
			item.UserID = varUserID;
			
		
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}

		
		/// <summary>
		/// Updates a record, can be used with the Object Data Source
		/// </summary>
		public static void Update(int varId,Guid varRoleID,Guid varUserID)
		{
			UsersInRole item = new UsersInRole();
			
				item.Id = varId;
				
				item.RoleID = varRoleID;
				
				item.UserID = varUserID;
				
			item.IsNew = false;
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}

		#endregion
		#region Columns Struct
		public struct Columns
		{
			 public static string Id = @"ID";
			 public static string RoleID = @"RoleID";
			 public static string UserID = @"UserID";
						
		}

		#endregion
	}

}

